#include "qhistorydatabaseview.h"

#include "qdatabaselistview.h"
#include "qdatapanelwidget.h"

#include "../minisplitter.h"
#include "../styledbar.h"
#include "../qactiontoolbar.h"
#include "../qdeviceinformationdialog.h"

#include "../../shared/sqlite/qsqlitecontrol.h"

#include <QVBoxLayout>
#include <QSqlQuery>

QHistoryDatabaseView::QHistoryDatabaseView(QWidget *parent) :
    QWidget(parent),
    m_data_list_view(new QDataBaseListView),
    m_sqlite_control(new QSqliteControl),
    m_data_panel_widget(new QDataPanelWidget)
{
    MiniSplitter *s=new MiniSplitter;

    m_styledbar=new StyledBar;

    QVBoxLayout *vv=new QVBoxLayout;
    vv->setMargin(0);
    vv->setSpacing(0);
    vv->addWidget(m_styledbar);
    vv->addWidget(m_data_list_view);

    QWidget* wid=new QWidget;
    wid->setLayout(vv);

    s->addWidget(wid);
    s->addWidget(m_data_panel_widget);
    s->setStretchFactor(0,0);
    s->setStretchFactor(1,1);
    s->setSizes(QList<int>()<<150<<300);

    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);

    v->addWidget(s);

    setLayout(v);

    connect(m_data_list_view,SIGNAL(select(QString)),this,SLOT(select(QString)));

    setProperty("panelwidget",true);
}

QHistoryDatabaseView::~QHistoryDatabaseView()
{
    clear();
    delete m_sqlite_control;
}

void QHistoryDatabaseView::set_toolbar(QWidget *wid)
{
    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);
    v->addWidget(wid);
    m_styledbar->setLayout(v);

    QActionToolBar *bar=(QActionToolBar*)wid;

    QAction *ac=new QAction(this);
    ac->setText(tr("Information"));
    connect(ac,SIGNAL(triggered()),this,SLOT(show_info()));
    connect(this,SIGNAL(has_data(bool)),ac,SLOT(setEnabled(bool)));
    ac->setIcon(QIcon(":/images/info.png"));
    ac->setEnabled(false);
    bar->addButton(ac);
}

bool QHistoryDatabaseView::load(const QString &path)
{
    clear();
    if(!m_sqlite_control->open(path))
    {
        return false;
    }
    QStringList list=m_sqlite_control->get_tables();
    list.removeAll("property");

    QStringList l;
    QString t;

    foreach(const QString &str,list)
    {
        QSqlQuery query=m_sqlite_control->exec("select * from ["+str+"]");
        query.next();
        t=query.value(1).toString()+"."+query.value(0).toString();
        l.append(t);
        m_name_to_uuid.insert(t,str);
        QSqlQuery* que=new QSqlQuery(query);
        m_name_to_query.insert(t,que);
    }
    m_data_list_view->load(l);
    emit has_data(true);
    return true;
}

void QHistoryDatabaseView::clear()
{
    qDeleteAll(m_name_to_query.values());
    m_name_to_uuid.clear();
    m_name_to_query.clear();
    m_data_list_view->clear();
    m_sqlite_control->close();
    m_data_panel_widget->clear();
    emit has_data(false);
}

void QHistoryDatabaseView::select(const QString &name)
{
    QSqlQuery *query=m_name_to_query.value(name);
    if(query==NULL)
    {
        return;
    }
    m_data_panel_widget->set_query(query);
}

void QHistoryDatabaseView::show_info()
{
    QSqlQuery query=m_sqlite_control->exec("select * from property");

    QDeviceInformationDialog dlg(this);

    while(query.next())
    {
        dlg.set_property(query.value(0).toString(),query.value(1).toString());
    }

    dlg.exec();
}
